package com.suse.service.community.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.suse.service.community.constant.RoleConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * <p>
 * 用户
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-29
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value="User", description="用户")
public class User implements Serializable , UserDetails {

    private static final long serialVersionUID = 1L;

    private Integer id;

    private String username;

    private String password;

    private String salt;

    private String email;

    @ApiModelProperty(value = "0-普通用户; 1-超级管理员; 2-版主;")
    private Integer type;

    @ApiModelProperty(value = "0-未激活; 1-已激活;")
    private Integer status;

    private String activationCode;

    private String headerUrl;

    private Date createTime;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        if (type == 0) {
            authorities.add(new SimpleGrantedAuthority(RoleConstant.USER));
        }
        if (type == 1) {
            authorities.add(new SimpleGrantedAuthority(RoleConstant.ADMIN));
        }
        if (type == 2) {
            authorities.add(new SimpleGrantedAuthority(RoleConstant.MODERATOR));
        }
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

}
